<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>底部导航</title>
		<script type="text/javascript" src="../script/api.js"></script>
		<script type="text/javascript" src="../script/jquery.js"></script>
		<style>
			table {
				margin-left: 5px;
				margin-right: 5px;
				width: calc(100% - 10px);
				text-align: center;
				border: 0px;
				font: caption;
			}
			table tr {
				border: 1px solid #f4f4f4;
				padding: 5px;
			}
			table tr td {
				width: 32%;
				border: 1px solid #f4f4f4;
				padding: 5px
			}
			.tr1 {
				background-color: #f4f4f4;
			}
			.one {
				width: 100%;
				border-bottom: 1px solid #eee;
				padding: 5px;
			}
			.one_zi {
				margin-left: 10px;
				margin-top: 5px;
				font-size: 12px;
			}
			body {
				background: #FFF;
			}
		</style>
	</head>
	<body >
		<input type="button" onclick="scanDevice()" value="搜索设备"/>
		<input type="button" onclick="connectedDevice()" value="conn"/>
		<input type="button" onclick="startNotifyDevice()" value="notify"/>
		<input type="button" onclick="stopNotifyDevice()" value="stopnotify"/>
		<input type="button" onclick="stopDevice()" value="停止搜索设备"/>
		<input type="button" onclick="readDevice('','')" value="电量"/>
		<input type="button" onclick="writeDevice()" value="data"/>
		<table class="grxx_top_biaoge" border="0" cellpadding="0" cellspacing="0"  style="width:95%;">
			<tr>
				<th>设备编号</th>
				<th >匹配设备</th>
			</tr>
			<tr>
				<td >腰</td>
				<td id="k_head" >未匹配</td>
			</tr>
			<tr>
				<td >膝</td>
				<td  id="k_knee">未匹配</td>
			</tr>
			<tr>
				<td >足</td>
				<td  id="k_font">未匹配</td>
			</tr>
		</table>
		<script>
			var scanDeviceModule = null;
			var cur_param = {
				devices : [{
					title : "测试设备",
					addr : "A0:E6:F8:4C:C6:C8",
					id : "head"
				}, {
					title : "测试设备",
					addr : "A0:E6:F8:4E:C1:DF",
					id : "knee"
				}, {
					title : "测试设备",
					addr : "A0:E6:F8:4E:B9:34",
					id : "font"
				}]
			};
			apiready = function() {
				$("#device_data").hide();
				scanDeviceModule = api.require('scanDeviceModule');
			}
			var uuid = null;
			cur_scan = true
			function scanDevice() {
				scanDeviceModule.scan(function(ret, err) {
					msg = ret;
				
					if (ret.status == 0) {
						if (cur_scan) {
							if (ret.addr == 'A0:E6:F8:4C:C6:C8') {
								$("#k_head").html('<span style="color:#f4c300">已匹配</span>')
								$("#k_head").attr("class", "matchs")
							}
							if (ret.addr == 'A0:E6:F8:4E:C1:DF') {
								$("#k_knee").html('<span style="color:#f4c300">已匹配</span>')
								$("#k_knee").attr("class", "matchs")
							}
							if (ret.addr == 'A0:E6:F8:4E:B9:34') {
								$("#k_font").html('<span style="color:#f4c300">已匹配</span>')
								$("#k_font").attr("class", "matchs")
							}
							if ($(".matchs").length == 3) {
								cur_scan = false;
							}
						}
					} else {
						alert(ret.msg)
					}
				});
			}

			function stopDevice() {
				alert("停止查询设备");
				scanDeviceModule.stop(function(ret, err) {
					alert("设备停止");
				});
			}

			function show(idx) {
				$('.all').hide();
				$('.div' + idx).show();
				$("#device_data").show();
			}

			cur_conn = true;
			function connectedDevice() {
		//	cur_param.showToast=true;
				scanDeviceModule.connect(cur_param, function(ret, err) {
					if (ret.status == 0) {
					if (cur_conn) {
					
							if (ret.addr == 'A0:E6:F8:4C:C6:C8') {
								$("#k_head").html('<span style="color:green;">已连接</span>')
								$("#k_head").attr("class", "conns")
							}
							if (ret.addr == 'A0:E6:F8:4E:C1:DF') {
								$("#k_knee").html('<span style="color:green;">已连接</span>')
								$("#k_knee").attr("class", "conns")
							}
							if (ret.addr == 'A0:E6:F8:4E:B9:34') {
								$("#k_font").html('<span style="color:green;">已连接</span>')
								$("#k_font").attr("class", "conns")
							}
							if ($(".conns").length == 3) {
								$("#lianjie").hide();
								$("#caishu").show();
								cur_conn = false;
							}
						}
					}else{
					alert(JSON.stringify(ret))
					}
					//$("#" + ret.addr).append("<div>" + ret.addr + ":" + ret.isConnected + "</div>")
				});
			}

			function startNotifyDevice() {
				var param = {
					uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					charUuid : "0000fff1-0000-1000-8000-00805f9b34fb"
				};
				scanDeviceModule.notify(param, function(ret, err) {
				alert(JSON.stringify(ret))
				});
			}
			function stopNotifyDevice() {
				///sysAlert("停止更新设备信息");
				var param = {
					uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					charUuid : "0000fff1-0000-1000-8000-00805f9b34fb"
				};
				scanDeviceModule.stopNotify(param, function(ret, err) {

					if (JSON.stringify(ret) == "{}"||ret.status==1) {
						alert("链接已断开数据丢失,请重新搜索设备进行采集！");
				
						$("#k_head").text("未匹配");
						$("#k_knee").text("未匹配");
						$("#k_font").text("未匹配");
	cur_scan=true
	cur_conn=true
					} else {
						alert(JSON.stringify(ret))
					}
				});
			}
			function readDevice(s_uuid, c_uuid) {
				alert("读取设备信息");
				var param = {
					device_name : "测试设备",
					device_address : uuid,
					service_uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					characteristic_uuid : "0000FFF5-0000-1000-8000-00805F9B34FB"
				};
				scanDeviceModule.readDevice(param, function(ret, err) {
					$("#dl").text(_hex(parseInt(ret.hex.substr(2), 16)) + "%")
				});
			}

			function writeDevice() {
				api.prompt({
					buttons : ['确定', '取消']
				}, function(ret, err) {
					if (ret.text.length == 0) {
						alert("请输入你要改变的值")
					} else {
						if (ret.text == "1" || ret.text == "2" || ret.text == "3" || ret.text == "4") {
							var param = {
								device_name : "测试设备",
								device_address : uuid,
								service_uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
								characteristic_uuid : "0000fff5-0000-1000-8000-00805f9b34fb",
								writeContent : "0" + ret.text
							};
							scanDeviceModule.writeDevice(param, function(ret, err) {
								alert(JSON.stringify(ret));
							});
						} else {
							alert("请输入1或2或3或4");
						}
					}
				});
			}

			var service_uuid = null;
			var characteristic_uuid = null;
			function startNotifyDevice1(s_uuid, c_uuid) {
				service_uuid = s_uuid;
				characteristic_uuid = c_uuid;
				snd();
			}

			as = 0.000488
			bs = 0.07
			function snd() {
				var param = {
					device_name : "测试设备",
					device_address : uuid,
					service_uuid : service_uuid,
					characteristic_uuid : characteristic_uuid
				};
				scanDeviceModule.startNotifyDevice(param, function(ret, err) {
					$("td").css('color', '')
					//	document.getElementById("device_str").innerHTML = JSON.stringify(ret);
					var hex = ret.hex.substr(2).replace(/(\w{4})(?=\w)/g, "$1" + ",").split(",")
					x_acceleration = _hex(parseInt(hex[0], 16))
					y_acceleration = _hex(parseInt(hex[1], 16))
					z_acceleration = _hex(parseInt(hex[2], 16))
					x_angular = _hex(parseInt(hex[3], 16))
					y_angular = _hex(parseInt(hex[4], 16))
					z_angular = _hex(parseInt(hex[5], 16))
					$("#xx").text(_toData(x_acceleration, as));
					$("#yy").text(_toData(y_acceleration, as));
					$("#zz").text(_toData(z_acceleration, as));
					$("#x").text(_toData(x_angular, bs));
					$("#y").text(_toData(y_angular, bs));
					$("#z").text(_toData(z_angular, bs));
					$("#_data").text(_hex(parseInt(hex[6], 16)) + "")
				});
			}

			function _hex(d) {
				if (d == 0 || d == 32768) {
					return 0;
				} else {
					return d > 32767 ? d - 65536 : d
				}
			}

			function _toData(d, x) {
				return (d * x).toFixed(4)
			}
		</script>
	</body>
</html>
